#ifndef MISC_H
#define MISC_H

#include <QtCore>
#include "nbr.h"

class T_StopWatch
{
protected:
    bool m_active;
    int m_elapsed;
    QDateTime m_started;
public:
    explicit T_StopWatch()
        : m_active(false)
        , m_elapsed(0)
    {
    }
    bool isActive()
    {
        //FIXME
        return m_active;
    }
    void start()
    {
        if(m_active) return;
        m_started = QDateTime::currentDateTime();
        m_active = true;
    }
    void stop()
    {
        if(!m_active) return;
        QDateTime v_now = QDateTime::currentDateTime();
        m_elapsed += p_msecsTo(m_started, v_now);
        m_active = false;
    }
    void reset()
    {
        m_elapsed = 0;
        m_started = QDateTime::currentDateTime();
    }
    int elapsed()
    {
        //qDebug() << "T_StopWatch::elapsed()" << "[m_active]" << m_active << "[m_elapsed1]" << m_elapsed1;
        if(!m_active) return m_elapsed;
        QDateTime v_now = QDateTime::currentDateTime();
        //qDebug() << "T_StopWatch::elapsed()" << "[m_started]" << m_started << "[v_now]" << v_now;
        int v_result = m_elapsed + p_msecsTo(m_started, v_now);
        //qDebug() << "T_StopWatch::elapsed()" << "[v_result]" << v_result;
        return v_result;
    }
    void setElapsed(int a_msec)
    {
        m_elapsed = a_msec;
        m_started = QDateTime::currentDateTime();
    }
    QString toString(const QString &a_format)
    {
        QTime v_elapsed_clock(0, 0, 0, 0);
        v_elapsed_clock = v_elapsed_clock.addMSecs(this->elapsed());
        return v_elapsed_clock.toString(a_format);
    }
    quint64 p_msecsTo(QDateTime &a, QDateTime &b)
    {
        static const int c_daysToMillisecs = 24 * 3600 * 1000;
        int v_daysTo = a.date().daysTo(b.date());
        int v_msecsTo = a.time().msecsTo(b.time());
        if(v_daysTo == 0) return v_msecsTo;
        return v_msecsTo + (v_daysTo * c_daysToMillisecs);
    }
};

class T_FPS
{
public:
    T_StopWatch m_time_line;
    double m_fps;
    int m_count;
    int m_last_set;
    explicit T_FPS(bool dont_wake_now = false)
        : m_fps(0)
        , m_count(0)
        , m_last_set(0)
    {
        if(!dont_wake_now)
        {
            this->wake();
        }
    }
    void wake()
    {
        m_time_line.start();
    }
    void restart()
    {
        m_count = 0;
        m_time_line.reset();
        m_time_line.start();
    }
    void add(int a)
    {
        m_count += a;
    }
    void set(int a)
    {
        int v_diff = a - m_last_set;
        m_last_set = a;
        m_count += v_diff;
    }
    double value()
    {
        if(!m_time_line.isActive())
        {
            qDebug() << "[T_FPS.value()] warning: T_FPS.wake() was not called.";
            return 0;
        }
        else if(m_time_line.elapsed() < 1000)
        {
            return m_fps;
        }
        else
        {
            m_fps = 1000.0 * m_count / m_time_line.elapsed();
            restart();
            return m_fps;
        }
    }
};

#endif // MISC_H
